"use strict";
import {Utils} from '../util'

let utils = new Utils()
export const actions = {
  /**
   * 获取歌曲列表
   * @param commit
   * @param state
   * @param genera_id
   */
  getMailist ({commit, state}, genera_id) {
    let uri = '';
    if (state.genera > 0) {
      uri = '/mailist?genera_id=' + state.genera + '&page=' + state.page;
    }
    else if (state.search_lv > 0) {
      uri = '/mailist?lv_master=' + state.search_lv + '&page=' + state.page;
    }
    else if (state.search_word) {
      uri = '/mailist?title=' + state.search_word + '&page=' + state.page;
    }
    else {
      uri = '/mailist?page=' + state.page;
    }
    return new Promise((resolve, reject) => {
      utils.get(uri).then(res => {
          if (res.length > 0) {
            commit('SET_LIST', res);
            resolve();
          }
          else {
            reject({no_more: true});
          }
        }).catch(res => {
          reject(res);
      });
    });
  },

  getNodeDetail({commit, state}, nid) {
    return new Promise((resolve, reject) => {
      utils.get('/node/'+ nid +'?_format=json').then(detail_res => {
        utils.get('/field_collection_item/'+ detail_res.field_mai_score[0].value +'?_format=json').then(field_collction_res => {
          commit('SET_DETAIL', detail_res);
          commit('SET_LV_DETAIL', field_collction_res);
          resolve()
        });
      }).catch(res => {
        reject(res);
      });
    });
  },

  getScoreDetail({commit, state}, score_id) {
    return new Promise((resolve, reject) => {
      utils.get('/field_collection_item/'+ score_id +'?_format=json').then(res => {
        commit('SET_LV_DETAIL', res);
        resolve()
      }).catch(res => {
        reject(res);
      });
    });
  },
};